﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tunnel.Tools;

namespace Tunnel.DevicesControls
{
    public partial class TSForm : Form
    {
        public TSForm()
        {
            InitializeComponent();
        }
        TS ts = new TS();
        /// <summary>
        /// 交通信号灯控制窗体构造方法，主要用以将交通信号灯TS类型对象传递至该窗体
        /// </summary>
        /// <param name="t">被传递的交通信号灯TS类型对象</param>
        public TSForm(TS t)
        {
            InitializeComponent();
            ts = t;

            switch(ts.StatusDescription)
            {
                case "红灯": rbRed.Checked = true; break;
                case "黄灯": rbYellow.Checked = true; break;
                case "绿灯": rbGreen.Checked = true; break;
                case "全灭": rbLeft.Checked = true; break;                
            }

            name.Text = ts.Name;
            pos2.Text = ts.DevicePos;


            if(!Global.PlcStatus[ts.Protocol])
            {
                OK.Enabled = false;
            }
            textBox1.Text = ts.GreenID + "\r\n" + ts.RedID + "\r\n" + ts.YellowID + "\r\n" + ts.TurnID;

            
        }
        /// <summary>
        /// 点击确定按钮后触发的方法。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OK_Click(object sender, EventArgs e)
        {
            if (rbRed.Checked == true)
            {
                //ts.StatusDescription = "红灯";
                bwRedCommand.RunWorkerAsync();
                this.Close();
                return;
            }
            if (rbYellow.Checked == true)
            {
                //ts.StatusDescription = "黄灯";
                bwYellowCommand.RunWorkerAsync();
                this.Close();
                return;
            }
            if (rbGreen.Checked == true)
            {
                //ts.StatusDescription = "绿灯";
                bwGreenCommand.RunWorkerAsync();
                this.Close();
                return;
            }
            if (rbLeft.Checked == true)
            {
                //ts.StatusDescription = "全灭";
                bwOffCommand.RunWorkerAsync();
                this.Close();
                return;
            }

            MessageBox.Show("请选择红绿灯状态");
        }
        /// <summary>
        /// 后台进程，设置为红灯。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bwRedCommand_DoWork(object sender, DoWorkEventArgs e)
        {
            //string sql1 = "update R_RunStatus set ParamValue='0',UpdateTime=GETDATE(),Runned='0' where RSID='" + ts.GreenID + "'";
            //string sql2 = "update R_RunStatus set ParamValue='1',UpdateTime=GETDATE(),Runned='0' where RSID='" + ts.RedID + "'";
            //string sql3 = "update R_RunStatus set ParamValue='0',UpdateTime=GETDATE(),Runned='0' where RSID='" + ts.TurnID + "'";
            //string sql4 = "update R_RunStatus set ParamValue='0',UpdateTime=GETDATE(),Runned='0' where RSID='" + ts.YellowID + "'";

            //string[] sqlTranCommands = { sql1, sql2, sql3, sql4 };

            Global g = new Tunnel.Global();
            string CommandID = g.CommandID;
            string redcommand = "INSERT INTO R_Command(CommandID, DeviceID, ParamName, ParamValue, UpdateTime, UpdateBy, Runned) VALUES('" + CommandID + "', '" + ts.DeviceID + "', '" + ts.RedID + "', '1', GETDATE(), '" + Global.User + "', 0)";

            SqlDBHelper sdh = new SqlDBHelper();

            sdh.ExecuteCommand(redcommand);

            LogDB.CommandLog("TS", ts.DeviceID, "切换为红灯");
        }
        /// <summary>
        /// 后台进程，设置为黄灯。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bwYellowCommand_DoWork(object sender, DoWorkEventArgs e)
        {
            //string sql1 = "update R_RunStatus set ParamValue='0',UpdateTime=GETDATE(),Runned='0' where RSID='" + ts.GreenID + "'";
            //string sql2 = "update R_RunStatus set ParamValue='0',UpdateTime=GETDATE(),Runned='0' where RSID='" + ts.RedID + "'";
            //string sql3 = "update R_RunStatus set ParamValue='0',UpdateTime=GETDATE(),Runned='0' where RSID='" + ts.TurnID + "'";
            //string sql4 = "update R_RunStatus set ParamValue='1',UpdateTime=GETDATE(),Runned='0' where RSID='" + ts.YellowID + "'";

            //string[] sqlTranCommands = { sql1, sql2, sql3, sql4 };

            Global g = new Tunnel.Global();
            string CommandID = g.CommandID;
            string yellowcommand = "INSERT INTO R_Command(CommandID, DeviceID, ParamName, ParamValue, UpdateTime, UpdateBy, Runned) VALUES('" + CommandID + "', '" + ts.DeviceID + "', '" + ts.YellowID + "', '1', GETDATE(), '" + Global.User + "', 0)";

            SqlDBHelper sdh = new SqlDBHelper();

            sdh.ExecuteCommand(yellowcommand);
            LogDB.CommandLog("TS", ts.DeviceID, "切换为黄灯");
        }
        /// <summary>
        /// 后台进程，设置为绿灯。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bwGreenCommand_DoWork(object sender, DoWorkEventArgs e)
        {
            //string sql1 = "update R_RunStatus set ParamValue='1',UpdateTime=GETDATE(),Runned='0' where RSID='" + ts.GreenID + "'";
            //string sql2 = "update R_RunStatus set ParamValue='0',UpdateTime=GETDATE(),Runned='0' where RSID='" + ts.RedID + "'";
            //string sql3 = "update R_RunStatus set ParamValue='0',UpdateTime=GETDATE(),Runned='0' where RSID='" + ts.TurnID + "'";
            //string sql4 = "update R_RunStatus set ParamValue='0',UpdateTime=GETDATE(),Runned='0' where RSID='" + ts.YellowID + "'";

            //string[] sqlTranCommands = { sql1, sql2, sql3, sql4 };

            Global g = new Tunnel.Global();
            string CommandID = g.CommandID;
            string greencommand = "INSERT INTO R_Command(CommandID, DeviceID, ParamName, ParamValue, UpdateTime, UpdateBy, Runned) VALUES('" + CommandID + "', '" + ts.DeviceID + "', '" + ts.GreenID + "', '1', GETDATE(), '" + Global.User + "', 0)";

            SqlDBHelper sdh = new SqlDBHelper();

            sdh.ExecuteCommand(greencommand);
            LogDB.CommandLog("TS", ts.DeviceID, "切换为绿灯");
        }
        /// <summary>
        /// 后台进程，熄灭全部灯。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bwOffCommand_DoWork(object sender, DoWorkEventArgs e)
        {
            //string sql1 = "update R_RunStatus set ParamValue='0',UpdateTime=GETDATE(),Runned='0' where RSID='" + ts.GreenID + "'";
            //string sql2 = "update R_RunStatus set ParamValue='0',UpdateTime=GETDATE(),Runned='0' where RSID='" + ts.RedID + "'";
            //string sql3 = "update R_RunStatus set ParamValue='0',UpdateTime=GETDATE(),Runned='0' where RSID='" + ts.TurnID + "'";
            //string sql4 = "update R_RunStatus set ParamValue='0',UpdateTime=GETDATE(),Runned='0' where RSID='" + ts.YellowID + "'";

            //string[] sqlTranCommands = { sql1, sql2, sql3, sql4 };

            //SqlDBHelper sdh = new SqlDBHelper();

            //sdh.ExecteTranCommand(sqlTranCommands);
            Global g = new Tunnel.Global();
            string CommandID = g.CommandID;
            string turncommand = "INSERT INTO R_Command(CommandID, DeviceID, ParamName, ParamValue, UpdateTime, UpdateBy, Runned) VALUES('" + CommandID + "', '" + ts.DeviceID + "', '" + ts.TurnID + "', '1', GETDATE(), '" + Global.User + "', 0)";

            SqlDBHelper sdh = new SqlDBHelper();

            sdh.ExecuteCommand(turncommand);
            LogDB.CommandLog("TS", ts.DeviceID, "切换为左转");
        }

        private void quit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
